package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.DateUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Description: No Description
 * User: Eric
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("/getLoginUsername")
    public Result getLoginUsername(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
    }

    public static void main(String[] args) {
        Calendar car = Calendar.getInstance();

        car.add(Calendar.YEAR,-1);
        for(int i=0; i < 12; i++){
            car.add(Calendar.MONTH,1);
            System.out.println(car.getTime());
        }
    }

    @RequestMapping("/loginSuccess")
    public Result loginSuccess(){
        return new Result(true, "登陆成功");
    }

    @RequestMapping("/loginFail")
    public Result loginFail(){
        return new Result(false, "登陆失败");
    }

    /**
     * 编辑用户信息
     * @param user
     * @param roleIds
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody User user,Integer[] roleIds){
        userService.update(user,roleIds);
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    /**
     * 新增用户信息
     * @param user
     * @param roleIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody User user,Integer[] roleIds){
        userService.add(user,roleIds);
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    /**
     * 分页查询用户信息
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<User> pageResult = userService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS,pageResult);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id){
        User user= userService.findById(id);
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);
    }

    /**
     * 根据id查询用户角色信息
     * @param id
     * @return
     */
    @GetMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(int id){
        List<Integer> roleIds = userService.findRoleIdsByUserId(id);
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS,roleIds);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(int id){
        userService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }
}
